home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5718 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  91 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.csug.rochester.edu!rit!kar
  3. From: kar@cs.rit.edu (Kenneth A Reek)
  4. Subject: Re: accessing structures (newbie question)
  5. Message-ID: <1996Feb20.140043.14285@cs.rit.edu>
  6. Sender: news@cs.rit.edu (USENET News Admin)
  7. Nntp-Posting-Host: madrid
  8. Organization: Rochester Institute of Technology, Rochester, NY
  9. References: <4g8gic$o6u@news1.sunbelt.net> <3128FA60.5227@metagen.co.uk>
  10. Date: Tue, 20 Feb 1996 14:00:43 GMT
  11.  
  12. In article <3128FA60.5227@metagen.co.uk>, Rob Stenton <rws@metagen.co.uk> writes:
  13. > There are basically two ways of accessing elements within a malloced 
  14. > buffer:
  15. > 1. Treat the malloced buffer as an array (as you suggested).
  16. > 2. Increment a local pointer by sizeof(picture) in loop iteration.
  17. > The first method is preferable because the second method involves pointer 
  18. > arithmetic (always best avoided) which can cause problems on different 
  19. > memory architectures and assumes things about BYTE sizes (but usually 
  20. > works).
  21.  
  22.     The ANSI standard specifies the behavior of pointer arithmetic, so
  23. there is no problem using it on any specific architecture.  Problems caused
  24. by different sizes of things such as integers can occur if you take the
  25. data generated on one architecture, byte for byte, and try to interpret it
  26. on an architecture with different size data.  However, a correct program
  27. on one architecture will also run correctly on the other, even though the
  28. byte for byte image of the data that is created may differ.
  29.  
  30. > I think you may have been using the wrong syntax when you tried the array 
  31. > method. The xth item is given by 'photos[x].' which is equivalent to 
  32. > '(photos + x * sizeof(picture))->'.
  33.  
  34.     Conceptually true, but actually incorrect.  When pointer arithmetic
  35. is done, the compiler scales values added to a pointer by the size of the
  36. thing it points to.  The intent of the expression above is actually achieved
  37. with this expression:
  38.  
  39.     ( photos + x )->
  40.  
  41. > The two ways for solving this problem are therefore:
  42. > 1. (Recommended)
  43. >          for (x=0;x<num_of_files;x++)
  44. >          {
  45. >                  strncpy(photos[x].filename, filelist[x],12);
  46. >                  strncpy(photos[x].diskname, inputdisk,12);
  47. >                  strncpy(photos[x].indexname, inputindex,12);
  48. >          }
  49.  
  50.     Straightforward and correct.
  51.  
  52. > or
  53. > 2. (Not recommended)
  54. >          for (x=0;x<num_of_files;x++)
  55. >          {
  56. >                  strncpy((photos + x * sizeof(picture))->filename, 
  57. > filelist[x],12);
  58. >                  strncpy((photos + x * sizeof(picture))->diskname, 
  59. > inputdisk,12);
  60. >                  strncpy((photos + x * sizeof(picture))->indexname, 
  61. > inputindex,12);
  62. >          }
  63.  
  64.     Incorrect due to the scaling done by the compiler with pointer
  65. arithmetic, as described above.  But even if that were fixed, this use
  66. of pointers has no advantage over the subscript example above.  To get
  67. greater efficiency, use a copy of the pointer and increment it:
  68.  
  69.     struct picture *pp;    /* or whatever, I don't remember the
  70.                 original poster's structure */
  71.     ...
  72.     for( pp = photos; pp < photos + num_of_files; pp++ ){
  73.         strncpy( pp->filename, ... );
  74.         strncpy( pp->diskname, ... );
  75.         strncpy( pp->indexname, ... );
  76.     }
  77.  
  78. The pp++ in the for statement increments pp by the right amount so that
  79. it points to the next structure in the array.  If speed is your primary
  80. concern, declare a second pointer variable and initialize it to
  81.  
  82.     photos + num_of_files
  83.  
  84. and use that in the for statement rather than the expression.
  85.  
  86.  
  87. Kenneth A. Reek                kar@cs.rit.edu
  88. Rochester Institute of Technology    716-475-6155 (voice)
  89. Rochester, NY 14623-0887 USA        716-475-7100 (fax)
  90.